//
//  GetChatTime.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/5/10.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "GetChatTime.h"


@implementation GetChatTime
/**
 聊天时间转换器，非常好用（调用此方法）
 
 @param TimeInterval 时间，传入毫秒，里面自动转为秒（double类型）
 @param needTime 是否显示昨天今天后面的时间
 @return 时间字符串
 */
+(NSString *)getMessageDateStringFromTimeInterval:(NSTimeInterval)TimeInterval andNeedTime:(BOOL)needTime
{
    //毫秒转秒（必须，否则死循环）
    TimeInterval= TimeInterval/1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:TimeInterval];
    return [GetChatTime getMessageDateString:date andNeedTime:needTime];
}

+ (NSString*)getMessageDateString:(NSDate*)messageDate andNeedTime:(BOOL)needTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale systemLocale]];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
   // 2020-05-09 15:32:03 后台时间格式
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:messageDate];
    NSDate *msgDate = [cal dateFromComponents:components];
    
    NSString*weekday = [GetChatTime getWeekdayWithNumber:components.weekday];
    
    components = [cal components:NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    
    if([today isEqualToDate:msgDate]){
        if (needTime) {
            [formatter setDateFormat:@"HH:mm:ss"];
        }
        else{
            [formatter setDateFormat:@""];
        }
        return [formatter stringFromDate:messageDate];
    }
    
    components.day -= 1;
    NSDate *yestoday = [cal dateFromComponents:components];
    if([yestoday isEqualToDate:msgDate]){
        if (needTime) {
            [formatter setDateFormat:@"昨天 HH:mm:ss"];
        }
        else{
            [formatter setDateFormat:@"昨天"];
        }
        return [formatter stringFromDate:messageDate];
    }
#pragma mark ========= 修改代码为 i <= 5，修改前 i <= 6=============
//    for (int i = 1; i <= 5; i++) {
//        components.day -= 1;
//        NSDate *nowdate = [cal dateFromComponents:components];
//        if([nowdate isEqualToDate:msgDate]){
//            if (needTime) {
//                [formatter setDateFormat:[NSString stringWithFormat:@"%@ HH:mm:ss",weekday]];
//            }
//            else{
//                [formatter setDateFormat:[NSString stringWithFormat:@"%@",weekday]];
//            }
//            return [formatter stringFromDate:messageDate];
//        }
//    }
    
    while (1) {
        components.day -= 1;
        NSDate *nowdate = [cal dateFromComponents:components];
        if ([nowdate isEqualToDate:msgDate]) {
            if (!needTime) {
                [formatter setDateFormat:@"YYYY:MM:dd"];
            }
            return [formatter stringFromDate:messageDate];
            break;
        }
   }
    
}

//1代表星期日、如此类推
+(NSString *)getWeekdayWithNumber:(long)number
{
    switch (number) {
        case 1:
            return @"星期日";
            break;
        case 2:
            return @"星期一";
            break;
        case 3:
            return @"星期二";
            break;
        case 4:
            return @"星期三";
            break;
        case 5:
            return @"星期四";
            break;
        case 6:
            return @"星期五";
            break;
        case 7:
            return @"星期六";
            break;
        default:
            return @"";
            break;
    }
}



////计算每条消息是否显示时间
//dataArray:UITableView的数据源 arr:历史消息
-(NSMutableArray*)computeVisibleTimeAndRefreshTable:(NSString *)type dataArr:(NSMutableArray*)dataSouce historyArr:(NSArray*)historyArr{

    NSTimeInterval  lastVisibleTime = 0;
    for (NSInteger i=0; i<dataSouce.count; i++) {
       MantisCellBaseModel* modeTime = dataSouce[i];
        if (i==0) {
         //一定显示时间
            modeTime.isTimeVisible=true;
            //最后的时间
            NSTimeInterval nowTimer = [[NSString jk_UUIDTimestamp]doubleValue];
            if (modeTime.sendTime==0) {
                modeTime.sendTime=nowTimer;
            }
            lastVisibleTime = modeTime.sendTime;
        
        }else{
         
           //计算时间差，毫秒（5分钟）
            long long timeSpan = modeTime.sendTime - lastVisibleTime;
            if (timeSpan>=maxMinute*60*1000){
                //一定显示时间
                modeTime.isTimeVisible=true;
                //最后的时间
                lastVisibleTime = modeTime.sendTime;
            }else{
                //不展示时间
                modeTime.isTimeVisible=false;
            }
           
        }
      
      //时间设置完成后数组替换
      dataSouce[i]=modeTime;
    }
    return dataSouce;
}

+(NSTimeInterval)getTimeStampFrom:(NSString*)pstrTime
{
    NSDateFormatter *pFormatter= [[NSDateFormatter alloc]init];
    [pFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *pCurrentDate = [pFormatter dateFromString:pstrTime];
    return [pCurrentDate timeIntervalSince1970] * 1000;
}




@end
